package com.gss.stockLedger.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.gss.stockLedger.domain.StockTransactionDtls;
import com.gss.stockLedger.form.ReportGenerateForm;

public class QueryStockNoDaoSupportDAO {
	
	@Autowired
	SessionFactory factory2;
	
	@Autowired
	SessionFactory sessionFactory;
	
	Logger logger = Logger.getLogger(QueryStockNoDaoSupportDAO.class);
	
	public List<StockTransactionDtls> getStockTransactionsForProduct(ReportGenerateForm	reportForm,String partId,String inventoryType)throws Exception
	{
		Session session = null;
		Transaction transaction = null;
		
		try{
			if(reportForm.getCompanyName().equalsIgnoreCase("RNAIPL")){
				session = sessionFactory.openSession();
			}
			else if(reportForm.getCompanyName().equalsIgnoreCase("NMIPL")){
				session =  factory2.openSession();
			}
			transaction = session.beginTransaction();
			Order order = Order.asc("transactionDate");
			Order order2 = Order.asc("assignmentId");
			Order order3 = Order.desc("transactionType");
			Criteria criteria = session.createCriteria(StockTransactionDtls.class).add(Restrictions.eq("partId", partId)).add(Restrictions.eq("inventoryType", inventoryType)).addOrder(order).addOrder(order2).addOrder(order3);
			
			if(null != reportForm.getEndDate()){
//				criteria.add(Restrictions.le("transactionDate", reportForm.getEndDate()));
				criteria.add(Restrictions.le("transactionDate", reportForm.getSecEndDate()));
			}			
			
			/*Added on 06 Jun 2012*/
			if(null != reportForm.getPurchaseType() && !reportForm.getPurchaseType().equalsIgnoreCase("All")){
				System.out.println("filter : PURCHASE TYPE : applied");
				criteria.add(Restrictions.eq("purchaseType", reportForm.getPurchaseType().trim()));
			}
			if(null != reportForm.getTransactionType() && !reportForm.getTransactionType().equalsIgnoreCase("All")){
				criteria.add(Restrictions.eq("transactionType", reportForm.getTransactionType().trim()));
				System.out.println("filter : TRANSACTION TYPE : applied");
			}
			if(null != reportForm.getAccountType() && !reportForm.getAccountType().equalsIgnoreCase("All")){
				criteria.add(Restrictions.eq("acctType", reportForm.getAccountType().trim()));
				System.out.println("filter : ACCOUNT TYPE : applied");
			}
			/**/
			return criteria.list();
		}
		catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<StockTransactionDtls>();
		}
		finally
		{
			if(null != transaction)
				transaction.commit();
			if(session!=null&&session.isOpen())
			{
				session.close();
			}
		}
	}
	
	public List getUniquePartIds(String invType,String partId, String companyName){
		Session session = null;
		Transaction transaction = null;
		
		try{
			if(companyName.equalsIgnoreCase("RNAIPL")){
				session = sessionFactory.openSession();
			}
			else if(companyName.equalsIgnoreCase("NMIPL")){
				session =  factory2.openSession();
			}
			else
				System.out.println(companyName);
		
			transaction = session.beginTransaction();
					
			ProjectionList projectionList = Projections.projectionList();
			Projection projection = Projections.distinct(Projections.property("partId"));
			projectionList.add(projection);
			projectionList.add(Projections.property("inventoryType"));
			Criteria criteria = session.createCriteria(StockTransactionDtls.class).setProjection(projectionList);
			if(invType!=null&&!invType.equalsIgnoreCase("All"))
			{
				criteria.add(Restrictions.eq("inventoryType", invType));
			}
			if(partId!=null && !"".equals(partId) && !partId.equalsIgnoreCase("All"))
			{
				criteria.add(Restrictions.eq("partId", partId));
			}
			List partIdList = criteria.list(); 
			return partIdList; 
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		finally{
			if(null != transaction)
				transaction.commit();
			if(session!=null && session.isOpen())
				{
					session.close();
				}
		}
		
	}
	
	public void uploadStock(List<StockTransactionDtls> stockTransactionDtls)
	{
		Session session =  factory2.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		for(StockTransactionDtls objToSave : stockTransactionDtls)
			session.save(objToSave);
		transaction.commit();
		session.close();
	}
}
